package com.rising.face.bytes.P6_爬楼梯;

/**
 * @Author: rising
 * @Description: 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
 *
 * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢？
 * @Date: create in 2023/1/18 10:20
 */
public class ClimbStairs {

    /**
     * 采用动态规划
     * dp[i] = dp[i-1] + dp[i-2]
     * @param sum
     * @return
     */
    public int stairs(int sum) {
        if (sum<=2) return sum;
        return stairs(sum -1) + stairs(sum - 2);
    }
    /**
     * 滑动窗口
     * @param n
     * @return
     */
    public int slidingArray(int n) {
        if (n<=2) return n;
        int sum = 0, first = 1, second = 2;
        for (int i = 3; i <= n; i++) {
            sum = first + second;
            first = second;
            second = sum;
        }
        return sum;
    }

    public static void main(String[] args) {
        ClimbStairs climbStairs = new ClimbStairs();
        System.out.println(climbStairs.stairs(1));
        System.out.println(climbStairs.stairs(2));
        System.out.println(climbStairs.stairs(3));
        System.out.println(climbStairs.stairs(4));
        System.out.println(climbStairs.stairs(5));
        long time1 = System.currentTimeMillis();
        System.out.println(climbStairs.stairs(45));//1836311903
        long time2 = System.currentTimeMillis();
        System.out.println("递归计算耗时，单位MS：" + (time2 - time1));
        System.out.println(climbStairs.slidingArray(45));//1836311903
        long time3 = System.currentTimeMillis();
        System.out.println("滑动数组计算耗时，单位MS：" + (time3 - time2));
    }
}
